<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="0 概述基础垃圾算法：标记-清除、标记-整理、复制 综合垃圾回收算法：分代收集算法、增量算法 1 标记-清除（Mark-Sweep）  标记需要回收的对象  清理掉要回收的对象   缺点：垃圾回收之后会存在内存碎片 2 标记-整理（Mark-Compact）  标记需要回收的对象  把所有的存活对象压缩的内存的一端  清理掉边界外的所有空间   这个算法可以避免标记-清除算法会产生的内存碎片 3">
<meta property="og:type" content="article">
<meta property="og:title" content="JVM 垃圾回收算法">
<meta property="og:url" content="http://littlefxc.github.io/2022/03/01/docs/JVM/JVM-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E7%AE%97%E6%B3%95/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="0 概述基础垃圾算法：标记-清除、标记-整理、复制 综合垃圾回收算法：分代收集算法、增量算法 1 标记-清除（Mark-Sweep）  标记需要回收的对象  清理掉要回收的对象   缺点：垃圾回收之后会存在内存碎片 2 标记-整理（Mark-Compact）  标记需要回收的对象  把所有的存活对象压缩的内存的一端  清理掉边界外的所有空间   这个算法可以避免标记-清除算法会产生的内存碎片 3">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/20220303214940.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/20220303222629.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20220303223424546.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20220311113729890.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20220311162205506.png">
<meta property="article:published_time" content="2022-03-01T01:19:01.000Z">
<meta property="article:modified_time" content="2022-05-10T01:24:23.763Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="jvm">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/littlefxc/oss/raw/master/images/20220303214940.png">


<link rel="canonical" href="http://littlefxc.github.io/2022/03/01/docs/JVM/JVM-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E7%AE%97%E6%B3%95/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>JVM 垃圾回收算法 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#0-%E6%A6%82%E8%BF%B0"><span class="nav-text">0 概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A0%87%E8%AE%B0-%E6%B8%85%E9%99%A4%EF%BC%88Mark-Sweep%EF%BC%89"><span class="nav-text">1 标记-清除（Mark-Sweep）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%A0%87%E8%AE%B0-%E6%95%B4%E7%90%86%EF%BC%88Mark-Compact%EF%BC%89"><span class="nav-text">2 标记-整理（Mark-Compact）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%A4%8D%E5%88%B6%EF%BC%88Copy%EF%BC%89"><span class="nav-text">3 复制（Copy）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%B8%89%E7%A7%8D%E7%AE%97%E6%B3%95%E5%AF%B9%E6%AF%94"><span class="nav-text">4 三种算法对比</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%88%86%E4%BB%A3%E6%94%B6%E9%9B%86%E7%AE%97%E6%B3%95"><span class="nav-text">5 分代收集算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E5%9B%9E%E6%94%B6%E7%B1%BB%E5%9E%8B"><span class="nav-text">5.1 回收类型</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E5%A2%9E%E9%87%8F%E7%AE%97%E6%B3%95"><span class="nav-text">6 增量算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%93%BE%E6%8E%A5"><span class="nav-text">链接</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2022/03/01/docs/JVM/JVM-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E7%AE%97%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JVM 垃圾回收算法
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-01 09:19:01" itemprop="dateCreated datePublished" datetime="2022-03-01T09:19:01+08:00">2022-03-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:24:23" itemprop="dateModified" datetime="2022-05-10T09:24:23+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="0-概述"><a href="#0-概述" class="headerlink" title="0 概述"></a>0 概述</h2><p>基础垃圾算法：标记-清除、标记-整理、复制</p>
<p>综合垃圾回收算法：分代收集算法、增量算法</p>
<h2 id="1-标记-清除（Mark-Sweep）"><a href="#1-标记-清除（Mark-Sweep）" class="headerlink" title="1 标记-清除（Mark-Sweep）"></a>1 标记-清除（Mark-Sweep）</h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/20220303214940.png"></p>
<ol>
<li><p>标记需要回收的对象</p>
</li>
<li><p>清理掉要回收的对象</p>
</li>
</ol>
<p>缺点：垃圾回收之后会存在内存碎片</p>
<h2 id="2-标记-整理（Mark-Compact）"><a href="#2-标记-整理（Mark-Compact）" class="headerlink" title="2 标记-整理（Mark-Compact）"></a>2 标记-整理（Mark-Compact）</h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/20220303222629.png"></p>
<ol>
<li><p>标记需要回收的对象</p>
</li>
<li><p>把所有的存活对象压缩的内存的一端</p>
</li>
<li><p>清理掉边界外的所有空间</p>
</li>
</ol>
<p>这个算法可以避免标记-清除算法会产生的内存碎片</p>
<h2 id="3-复制（Copy）"><a href="#3-复制（Copy）" class="headerlink" title="3 复制（Copy）"></a>3 复制（Copy）</h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20220303223424546.png" alt="image-20220303223424546"></p>
<ol>
<li><p>把内存分为两块，每次只使用一块</p>
</li>
<li><p>把当前使用的内存中的存活对象复制到未使用的内存中去，然后清除掉正在使用的内存中的所有对象</p>
</li>
<li><p>交换两个内存的角色，等待下次回收</p>
</li>
</ol>
<h2 id="4-三种算法对比"><a href="#4-三种算法对比" class="headerlink" title="4 三种算法对比"></a>4 三种算法对比</h2><table>
<thead>
<tr>
<th>回收算法</th>
<th>有点</th>
<th>缺点</th>
</tr>
</thead>
<tbody><tr>
<td>标记-清除</td>
<td>实现简单</td>
<td>存在内存碎片、分配内存速度会受影响</td>
</tr>
<tr>
<td>标记-整理</td>
<td>无碎片</td>
<td>整理内存存在开销</td>
</tr>
<tr>
<td>复制</td>
<td>性能好、无碎片</td>
<td>内存利用率低</td>
</tr>
</tbody></table>
<h2 id="5-分代收集算法"><a href="#5-分代收集算法" class="headerlink" title="5 分代收集算法"></a>5 分代收集算法</h2><ol>
<li><p>各种商业虚拟机堆内存的垃圾回收基本上都采用了垃圾回收</p>
</li>
<li><p>根据对象的存活周期，把内存分成多个区域，不同区域使用不同的回收算法回收对象</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20220311113729890.png" alt="image-20220311113729890"></p>
</li>
</ol>
<h3 id="5-1-回收类型"><a href="#5-1-回收类型" class="headerlink" title="5.1 回收类型"></a>5.1 回收类型</h3><ul>
<li>新生代回收（Minor GC|Young GC）</li>
<li>老年代回收（Major GC）</li>
<li>清理整个堆的回收（Full GC）</li>
</ul>
<p>一般的，发生 Major GC 时同时一般也会发生 Minor GC，所以 Major GC 约等于 Full GC。</p>
<p>对象分配过程见<a target="_blank" rel="noopener" href="https://blog.csdn.net/Little_fxc/article/details/118633548?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164697019016780366533422%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=164697019016780366533422&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-2-118633548.nonecase&utm_term=%E5%AF%B9%E8%B1%A1&spm=1018.2226.3001.4450">JVM之对象的创建过程</a></p>
<p>分代收集算法调优原则：</p>
<ul>
<li>合理设置 survivor 区域的大小，避免内存浪费；</li>
<li>GC尽量发生在新生代，尽量减少 Full GC 的发生。</li>
</ul>
<p>相关的JVM参数</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20220311162205506.png" alt="image-20220311162205506"></p>
<h2 id="6-增量算法"><a href="#6-增量算法" class="headerlink" title="6 增量算法"></a>6 增量算法</h2><p>如果内存非常大，如果一次性回收所有垃圾，势必会消耗很多的时间，从而造成系统长时间停摆。</p>
<ol>
<li>每次只收集一小片区域的内存空间的垃圾</li>
</ol>
<h2 id="链接"><a href="#链接" class="headerlink" title="链接"></a>链接</h2><ol>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Little_fxc/article/details/123210008?spm=1001.2014.3001.5501">垃圾回收</a></p>
<p>什么场景下该使用什么垃圾回收策略？垃圾回收发生在哪些区域？对象在什么时候能够被回收？</p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Little_fxc/article/details/122603669?spm=1001.2014.3001.5501">JVM分代收集理论</a></p>
<p>与章节 5 相关，主要介绍分代收集算法实现的理论依据</p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Little_fxc/article/details/114004380?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164698742116780269896838%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=164698742116780269896838&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-6-114004380.nonecase&utm_term=JVM&spm=1018.2226.3001.4450">JVM-内存结构</a></p>
<p>主要是基于分代收集理论的JVM内存结构简单分析，包括堆、虚拟机栈、本地方法栈、程序计数器和方法区</p>
</li>
</ol>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/jvm/" rel="tag"># jvm</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/03/01/docs/JVM/JVM-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6/" rel="prev" title="JVM 垃圾回收">
                  <i class="fa fa-chevron-left"></i> JVM 垃圾回收
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/03/11/docs/JVM/JVM-%E5%9E%83%E5%9C%BE%E6%94%B6%E9%9B%86%E5%99%A8/" rel="next" title="JVM 垃圾收集器">
                  JVM 垃圾收集器 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
